package com.bw.controller.material;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.entity.linkman.PiLinkman;
import com.bw.entity.material.PiMaterial;
import com.bw.resp.ResponseResult;
import com.bw.service.linkman.IPiLinkmanService;
import com.bw.service.material.IPiMaterialService;
import com.bw.utils.excel.ImportExcel;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxq
 * @since 2020-06-16
 */
@CrossOrigin
@RestController
@RequestMapping("/material")
public class PiMaterialController {

    @Autowired
    private IPiMaterialService materialService;

    @PostMapping("/list/{page}/{size}")
    public ResponseResult getList(@PathVariable("page") int page, @PathVariable("size") int size,@RequestBody PiMaterial material){
        ResponseResult result = ResponseResult.SUCCESS();

        try {
            PageHelper.startPage(page, size);
            QueryWrapper queryWrapper = new QueryWrapper();
            if (null!=material.getName()){
                queryWrapper.like("name","%"+material.getName()+"%");
            }
            if (null!=material.getChemical()){
                queryWrapper.like("chemical","%"+material.getChemical()+"%");
            }
            queryWrapper.eq("isdel",0);
            List<PiMaterial> materials = materialService.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(materials);
            result.setResult(pageInfo);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败:" + e.toString());
        }
        return result;
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody PiMaterial material){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            int unm = (int)((Math.random()*9+1)*100000);
            String h = "cl";
            material.setCode(h+unm);
            material.setIsdel(0);
            materialService.save(material);
            result.setResult("添加成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("添加失败"+e.toString());
        }
        return  result;
    }
    @DeleteMapping("/del/{id}")
    public ResponseResult del(@PathVariable long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
             PiMaterial material = materialService.getById(id);System.out.println("+++"+material);
             material.setIsdel(1);
             materialService.saveOrUpdate(material);
             QueryWrapper queryWrapper = new QueryWrapper();
             queryWrapper.eq("isdel",0);
             List<PiMaterial> list = materialService.list(queryWrapper);
             result.setResult(list);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("删除失败"+e.toString());
        }
        return  result;
    }
    @GetMapping("/get/{id}")
    public ResponseResult get(@PathVariable long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            System.out.println("++"+id);
           PiMaterial material =  materialService.getById(id);
            result.setResult(material);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return  result;
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody PiMaterial material){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            materialService.saveOrUpdate(material);
            result.setResult("修改成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("修改失败"+e.toString());
        }
        return  result;
    }

    @PostMapping("importExcel")
    public ResponseResult importExcel(MultipartFile file){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length());

            InputStream inputStream = file.getInputStream();

            List<Map> maps = ImportExcel.readExcel(ext, inputStream);

            String jsonString = JSON.toJSONString(maps);

            List<PiMaterial> piMaterials = JSON.parseArray(jsonString, PiMaterial.class);
            for (int i=0;i<piMaterials.size();i++){
                int unm = (int)((Math.random()*9+1)*100000);
                String h = "cl";
                piMaterials.get(i).setCode(h+unm);
            }

            materialService.saveBatch(piMaterials);

        }catch (Exception e){
            e.toString();
            result.setSuccess(false);
            result.setMessage("失败!");
        }
        return result;
    }

    @PostMapping("getSelectList")
    public ResponseResult getSelectList(){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            List<PiMaterial> list = materialService.list();
            result.setResult(list);
            result.setMessage("查询成功!");
        }catch (Exception e){
            result.setMessage("查询失败!"+e.toString());
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("getMaterialMessage/{id}")
    public ResponseResult getMaterialMessage(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PiMaterial material = materialService.getById(id);
            result.setResult(material);
            result.setMessage("查询成功!");
        }catch (Exception e){
            result.setMessage("查询失败!"+e.toString());
            result.setSuccess(false);
        }
        return result;
    }
}
